home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / benchmarks / write / write.c < prev   
Encoding:
C/C++ Source or Header  |  1989-09-15  |  2.3 KB  |  90 lines

  1. /* 
  2.  * write.c --
  3.  *
  4.  *    This program is a stand-alone benchmark to measure
  5.  *    the speed of writing a file.  It should be invoked
  6.  *    as follows:
  7.  *
  8.  *    write numK count
  9.  *
  10.  *    The program will write numK bytes to its standard
  11.  *    output, seeking back to zero and repeating count
  12.  *    times.  Then it will print out the write bandwidth.
  13.  *    If omitted, count defaults to 1.
  14.  *
  15.  * Copyright 1989 Regents of the University of California.
  16.  * Permission to use, copy, modify, and distribute this
  17.  * software and its documentation for any purpose and without
  18.  * fee is hereby granted, provided that the above copyright
  19.  * notice appear in all copies.  The University of California
  20.  * makes no representations about the suitability of this
  21.  * software for any purpose.  It is provided "as is" without
  22.  * express or implied warranty.
  23.  */
  24.  
  25. #ifndef lint
  26. static char rcsid[] = "$Header: /sprite/src/benchmarks/write/RCS/write.c,v 1.5 89/09/13 20:48:26 ouster Exp Locker: brent $ SPRITE (Berkeley)";
  27. #endif not lint
  28.  
  29. #include <stdio.h>
  30. #include <sys/time.h>
  31. #include <sys/resource.h>
  32.  
  33. static char buffer[16384];
  34.  
  35. main(argc, argv)
  36. int argc;
  37. char **argv;
  38. {
  39.     int total, repeats, numK, i;
  40.     double rate, micros;
  41.     struct rusage begin ,end;
  42.     struct timeval start, stop;
  43.     struct timezone tz;
  44.  
  45.     if (argc < 2) {
  46.     fprintf(stderr, "Usage: %s kBytes [count]\n", argv[0]);
  47.     exit(1);
  48.     }
  49.     numK = atoi(argv[1]);
  50.  
  51.     if (argc == 2) {
  52.     repeats = 1;
  53.     } else {
  54.     repeats = atoi(argv[2]);
  55.     }
  56.     total = 0;
  57.  
  58. #ifdef GETRUSAGE
  59.     getrusage(RUSAGE_SELF, &begin);
  60. #else
  61.     gettimeofday(&start, (struct timezone *) NULL);
  62. #endif
  63.  
  64.     for ( ; repeats > 0; repeats--) {
  65.     lseek(1, 0, 0);
  66.     for (i = numK; i > 0; ) {
  67.         if (i > 16) {
  68.         total += write(1, buffer, 16384);
  69.         i -= 16;
  70.         } else {
  71.         total += write(1, buffer, 1024*i - 1);
  72.         i = 0;
  73.         }
  74.     }
  75.     }
  76. #ifdef GETRUSAGE
  77.     getrusage(RUSAGE_SELF, &end);
  78.     micros = (end.ru_utime.tv_sec + end.ru_stime.tv_sec
  79.         - begin.ru_utime.tv_sec - begin.ru_stime.tv_sec)*1000000
  80.         + (end.ru_utime.tv_usec - begin.ru_utime.tv_usec)
  81.         + (end.ru_stime.tv_usec - begin.ru_stime.tv_usec);
  82. #else
  83.     gettimeofday(&stop, (struct timezone *) NULL);
  84.     micros = 1000000*(stop.tv_sec - start.tv_sec)
  85.         + stop.tv_usec - start.tv_usec;
  86. #endif
  87.     rate = total/(micros/1000000.0);
  88.     fprintf(stderr, "%d bytes written at %.0f bytes/sec.\n", total, rate);
  89. }
  90.